home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / editor / snmp_0_1.zip / snmp-0.1 / aka / snmp / Connect.java < prev    next >
Text File  |  1997-06-09  |  4KB  |  166 lines

  1. /*
  2. Snmp Library
  3. Copyright (C) 1997 Alex Kowalenko Associates Pty Ltd. All rights reserved.
  4.  
  5. This software maybe be free distributed, any any form, without fee, 
  6. but may not be modified in any way without express permission of 
  7. the directors of Alex Kowalenko Associates Pty Ltd. 
  8.  
  9. Alex Kowalenko Associates Pty Ltd makes no representations or
  10. warranties about the suitabililty of the software, not even the
  11. implied warranty of merchantability or fitness for any particular
  12. purpose.    
  13. */
  14.  
  15. package aka.snmp;
  16.  
  17. import java.net.*;
  18. import java.io.*;
  19.  
  20. /**
  21.  *
  22.  * This class implements a SNMP Unix/UDP Connection to an SNMP agent at
  23.  * specific address.
  24.  * <P>
  25.  * Connect is managed by ConnectManager.  One difference
  26.  * between Connect and ConnectManager is that 
  27.  * Connect deals only with arrays of bytes of characters and knows
  28.  * nothing about their contents.  ConnectManager deals with
  29.  * Pdu objects.
  30.  *
  31.  * @version     $Id: Connect.java,v 1.7 1997/05/18 07:34:11 alex Exp $
  32.  * @author      Alex Kowalenko
  33.  * @see ConnectManager
  34.  * @see Pdu
  35.  */
  36.  
  37. class Connect implements Runnable {
  38.  
  39.   public static final short SNMP_PORT = 161;
  40.   private static final short MAX_SNMP_LENGTH = 2048;
  41.   private static final byte sentinal[] = { 0x00};
  42.  
  43.   private String _address;
  44.   private int _timeOut;
  45.   private int _port;
  46.   private boolean _isOpen;
  47.   private DatagramSocket _socket;
  48.   private InetAddress _inetAddress;
  49.   private int _noActions;
  50.   
  51. /**
  52.  * Constructor
  53.  */
  54.  
  55.     Connect(String address, int timeOut, int port) 
  56.     throws UnknownHostException {
  57.         _address = address;
  58.         _inetAddress = InetAddress.getByName(address);
  59.         _timeOut = timeOut;
  60.         _port = port;
  61.     };
  62.  
  63. /**
  64.  * finalize
  65.  */
  66.  
  67.   protected void finalize() {
  68.       close();
  69.   };
  70.  
  71. /**
  72.  * Opens a connection
  73.  */
  74.     
  75.     boolean open() throws SnmpNetException, SocketException {
  76.     if(_isOpen)
  77.         throw new SnmpNetException("SnmpConnect.open already open");
  78.     
  79.     _socket = new DatagramSocket();
  80.     _isOpen = true;
  81.     return _isOpen;
  82.     };
  83.   
  84. /**
  85.  * Closes the connection 
  86.  */
  87.  
  88.     void close() {
  89.     _socket.close();
  90.     };
  91.   
  92. /**
  93.  * Is the connection currently open.
  94.  */
  95.     boolean isOpen() {
  96.     return _isOpen;
  97.     };
  98.   
  99. /**
  100.  * Send this sequence of characters to the other side, return 
  101.  * the response as a sequence of characters.
  102.  */
  103.  
  104.     ByteBuffer send(ByteBuffer msg) throws IOException, SnmpTimeOutException {
  105.     _noActions++;
  106.     // System.out.println("Send this: ");
  107.     // StringUtil.Hexdump(msg);
  108.     DatagramPacket packet 
  109.         = new DatagramPacket(msg.array(), msg.size(), _inetAddress, _port);
  110.     
  111.     _socket.send(packet);
  112.  
  113.     Thread thread = new Thread(this);
  114.     thread.start();
  115.     byte recvBuffer[] = new byte[MAX_SNMP_LENGTH];
  116.     DatagramPacket recvPacket 
  117.         = new DatagramPacket(recvBuffer, recvBuffer.length);
  118.     
  119.     try {
  120.         _socket.receive(recvPacket);
  121.     }
  122.     finally {
  123.         thread.stop();
  124.     };
  125.  
  126.     ByteBuffer result = new ByteBuffer(recvBuffer, recvPacket.getLength());
  127.     if(result.byteAt(0) == (byte) 0x00)
  128.         throw new SnmpTimeOutException("TimeOut to " + _address);
  129.     // StringUtil.Hexdump(result);
  130.     return result;
  131.     };
  132.  
  133. /** 
  134.  * This unblocks the socket when a packet is sent.  It writes a dummy
  135.  * message to the thread
  136.  */
  137.  
  138.   public void run() {
  139.       try {
  140.       Thread.currentThread().sleep(_timeOut * 1000);
  141.  
  142.       // Send a message to myself to wake the other thread
  143.       DatagramPacket packet 
  144.           = new DatagramPacket(sentinal, 
  145.                    sentinal.length, 
  146.                    InetAddress.getLocalHost(), 
  147.                    _socket.getLocalPort());
  148.       DatagramSocket socket = new DatagramSocket();
  149.       socket.send(packet);
  150.       } catch(Exception e) { }
  151.       finally {
  152.       Thread.currentThread().stop();
  153.       return;
  154.       }
  155.   };
  156.   
  157. /** 
  158.  * Cache information
  159.  */
  160.  
  161.     int count() {
  162.     return _noActions;
  163.     };
  164.  
  165. };
  166.